using System.Xml;
using Altova.TypeInfo;

namespace Altova.Xml
{
	public class TypeBase
	{
		protected internal XmlNode node;

		public XmlNode Node
		{
			get
			{
				return node;
			}
		}

		public TypeBase(XmlNode node)
		{
			this.node = node;
		}

		public XmlNode GetAttribute(MemberInfo member)
		{
			XmlElement xmlElement = (XmlElement)node;
			if (member.LocalName == "")
			{
				for (XmlNode xmlNode = xmlElement.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
				{
					if (xmlNode.NodeType == XmlNodeType.Text || xmlNode.NodeType == XmlNodeType.CDATA)
					{
						return xmlNode;
					}
				}
				return null;
			}
			return xmlElement.GetAttributeNode(member.LocalName, member.NamespaceURI);
		}

		public XmlNode CreateAttribute(MemberInfo member)
		{
			XmlElement xmlElement = (XmlElement)node;
			if (member.LocalName == "")
			{
				XmlNode attribute = GetAttribute(member);
				if (attribute == null)
				{
					XmlText xmlText = xmlElement.OwnerDocument.CreateTextNode("");
					xmlElement.AppendChild(xmlText);
					return xmlText;
				}
				return attribute;
			}
			XmlAttribute xmlAttribute = xmlElement.OwnerDocument.CreateAttribute(member.LocalName, member.NamespaceURI);
			xmlElement.SetAttributeNode(xmlAttribute);
			return xmlAttribute;
		}

		public void RemoveAttribute(MemberInfo member)
		{
			XmlElement xmlElement = (XmlElement)node;
			if (member.LocalName == "")
			{
				XmlNode xmlNode = xmlElement.FirstChild;
				while (xmlNode != null)
				{
					XmlNode xmlNode2 = xmlNode;
					xmlNode = xmlNode.NextSibling;
					if (xmlNode2.NodeType == XmlNodeType.Text || xmlNode2.NodeType == XmlNodeType.CDATA)
					{
						xmlElement.RemoveChild(xmlNode2);
					}
				}
			}
			else
			{
				xmlElement.RemoveAttribute(member.LocalName, member.NamespaceURI);
			}
		}

		public XmlNode GetElementFirst(MemberInfo member)
		{
			return GetElementAt(member, 0);
		}

		public XmlNode GetElementAt(MemberInfo member, int index)
		{
			for (XmlNode xmlNode = node.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
			{
				if (MemberEqualsNode(member, xmlNode) && index-- == 0)
				{
					return xmlNode;
				}
			}
			return null;
		}

		public XmlNode GetElementLast(MemberInfo member)
		{
			for (XmlNode xmlNode = node.LastChild; xmlNode != null; xmlNode = xmlNode.PreviousSibling)
			{
				if (MemberEqualsNode(member, xmlNode))
				{
					return xmlNode;
				}
			}
			return null;
		}

		public XmlNode CreateElement(MemberInfo member)
		{
			XmlDocument xmlDocument = node.OwnerDocument;
			if (xmlDocument == null)
			{
				xmlDocument = (XmlDocument)node;
			}
			XmlNode xmlNode = xmlDocument.CreateElement(member.LocalName, member.NamespaceURI);
			node.AppendChild(xmlNode);
			return xmlNode;
		}

		public int CountElement(MemberInfo member)
		{
			int num = 0;
			for (XmlNode xmlNode = node.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
			{
				if (MemberEqualsNode(member, xmlNode))
				{
					num++;
				}
			}
			return num;
		}

		public void RemoveElement(MemberInfo member)
		{
			XmlNode xmlNode = node.FirstChild;
			while (xmlNode != null)
			{
				XmlNode oldChild = xmlNode;
				xmlNode = xmlNode.NextSibling;
				if (MemberEqualsNode(member, oldChild))
				{
					node.RemoveChild(oldChild);
				}
			}
		}

		public void RemoveElementAt(MemberInfo member, int index)
		{
			XmlNode xmlNode = node.FirstChild;
			while (xmlNode != null)
			{
				XmlNode oldChild = xmlNode;
				xmlNode = xmlNode.NextSibling;
				if (MemberEqualsNode(member, oldChild) && index-- == 0)
				{
					node.RemoveChild(oldChild);
					break;
				}
			}
		}

		internal static bool MemberEqualsNode(MemberInfo member, XmlNode node)
		{
			string text = ((node.NamespaceURI == null) ? "" : node.NamespaceURI);
			string text2 = ((node.LocalName == null) ? "" : node.LocalName);
			string text3 = ((member.NamespaceURI == null) ? "" : member.NamespaceURI);
			string text4 = ((member.LocalName == null) ? "" : member.LocalName);
			if (text == text3 && text2 == text4)
			{
				return true;
			}
			return false;
		}

		public static int GetEnumerationIndex(MemberInfo member, string sValue, int enumOffset, int enumCount)
		{
			for (int i = enumOffset; i < enumOffset + enumCount; i++)
			{
				if (member.DataType.facets[i].facetName == "enumeration" && member.DataType.facets[i].stringValue == sValue)
				{
					return i - enumOffset;
				}
			}
			return -1;
		}
	}
}
